$.ajax({
        url: "/my/article/cates",
        success: function(res) {
            var str = template("pub", res)
            $("#select").html(str)
            layui.form.render()
        }

    })
    //初始化富文本
initEditor();
//图片裁剪
var $img = $(".left img")

var options = {
    //图片长宽比
    aspectRatio: 10 / 7,
    //预览元素选择器
    preview: ".preview"
}
$img.cropper(options)
    //选择图片
$(".chooseImg").on("click", function() {
        $(".file").click();
    })
    //设置裁剪图片
$(".file").on("change", function() {
    //获取图片对象
    var file = this.files[0];
    var imgURL = URL.createObjectURL(file)
    console.log(imgURL);
    //销毁原来的裁剪区域
    //改变图片地址
    //重新初始化裁剪区域
    $img.cropper("destroy").attr("src", imgURL).cropper(options)
})
var state = "已发布";
$(".save").on("click", function() {
    state = "草稿";
})
$(".pub-form").on("submit", function(e) {
    e.preventDefault();
    var fd = new FormData(this)
    fd.append("state", state)
    $img.cropper("getCroppedCanvas", {
        width: 400,
        height: 280
            //通过cancas对象。得到图片二进制对象
    }).toBlob(function(blob) {
        fd.append("cover_img", blob)
        postAtr(fd)
    })

})

function postAtr(fd) {
    $.ajax({
        url: "/my/article/add",
        method: "POST",
        data: fd,
        processData: false,
        contentType: false,
        success: function(res) {
            location.href = "/article/article_list.html"
        }
    })
}